
#ifndef HTMLFORMAT_H
#define HTMLFORMAT_H

#include <BFont>
#include <BTextEdit>
#include <BTextFormat>

using namespace BWE;

class HtmlFormat : public BTextFormat
{
public:
	struct Item
	{
		BXmlNode*		node = 0;
		BRect			rect;
		BFontHolder		font;
		BColor			color;
		BRange			range;
	};
	typedef BMap<int, Item>		ItemMap;

public:
	HtmlFormat(const BString& name = BString());
	~HtmlFormat();

	virtual void styled(const BStyle* style) override;
	virtual void changed(const BString& text) override;

	virtual const BFont* font(int pos) const;
	virtual const BColor* back(int pos) const;
	virtual const BColor* color(int pos) const;

protected:
	void insertItem(int pos, int end, const char* type);

	int readTagName(const BString& tagText, BString& name);
	void readAttribs(int begin, int end);

	bool readComment(BXmlNode* node, const BString& sontext, int& pos);
	bool readNode(BXmlNode* node, const BString& xmltext, int& pos);

protected:
	void slotAssetChanged();

private:
	BTextEdit*				_textEdit;
	BString					version;
	BString					encoding;
	BXmlNodeHolder			node;
	BXmlNodeHolderArray		topNodes;
	int						lineNumber;
	int						linePos;
	BMap<int, Item>			_itemMap;
	BMap<BString, BColor>	_colorMap;
};

#endif
